/*
 * @Descripttion: 
 * @version: 
 * @Author: turingchang
 * @Date: 2020-08-28 14:32:50
 * @LastEditors: turingchang
 * @LastEditTime: 2020-09-02 21:15:49
 */

'use strict';
const BaseController = require('../base/base');

class PermissionController extends BaseController {
    async list() {
        const ctx = this.ctx;
        var data = await ctx.service.admin.role.list();
        ctx.outSuccess(null, data);
    }
    async get() {
        const ctx = this.ctx;
        var id =ctx.query.id;
        var data = await ctx.service.admin.permission.get(id);
        ctx.outSuccess(null, data);
    }

    async menuTree(){
        // var parms ={
        //     page_index:ctx.query.pageNo,
        //     page_size:ctx.query.pageSize
        // }

        var data =await this.ctx.service.admin.permission.menuTree();
        this.ctx.outSuccess(null, data);
    }

    async getListByPid(){    
        var parms ={
            page_index: this.ctx.page_index,
            page_size:this.ctx.page_size,
            pid: this.ctx.params.pid
        }
        var data = await this.ctx.service.admin.permission.getListByPid(parms);
        data =this.ctx.convertToAntTableData(data);
        this.ctx.outSuccess(null, data);
    }

    async add(){
        const ctx = this.ctx;
        var post_data =ctx.request.body; 
        var result =false;
        if(ctx.app.utils.validator.isNull(post_data)){
            ctx.outError('添加失败',null);
            return;
        }
        // const { id,pid,key,name,type,path,icon,redirect,component,show,sort} =ctx.request.body; 
        const validateResult = await ctx.validate('admin.permission.add', post_data); 
        if (!validateResult) {
            ctx.outValidateStatus();
            return;
        }
        var d = await ctx.service.admin.permission.add(post_data);
        if(ctx.app.utils.validator.isNull(d)){
            ctx.outError('添加失败',null);
        }
        if(d.result == true){
            ctx.outSuccess(d.msg);
        }else{
            ctx.outError(d.msg,post_data,d.response_code_type);
        }
    }
    

}

module.exports = PermissionController;